﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;
using DataContext;

namespace QLCF.Forms
{
    public partial class popupAddNewProIntoQuantification : Form
    {
        public popupAddNewProIntoQuantification()
        {
            InitializeComponent();
        }
        private frmQuantification frmQuan = null;
        private uspProductGetInfoByProductIDResult productInfo = null;
        private int parentProductID = -1;
        private string parentName = "";
        public popupAddNewProIntoQuantification(int m_parentProductID,string m_parentPname,frmQuantification m_frmQuan)
        {
            InitializeComponent();
            parentProductID = m_parentProductID;
            parentName = m_parentPname;
            this.Text = this.Text + "\"" + m_parentPname + "\"";
            frmQuan = m_frmQuan;
            lstViewCate_LoadData();
        }
        private void lstViewCate_LoadData()
        {
            TreeNode node1 = new TreeNode();
            node1.Text = "Tất cả";
            node1.Tag = -1;
            node1.ImageIndex = 1;
            node1.SelectedImageIndex = 1;
            treeViewCate.Nodes.Add(node1);
            CtrCategory ctrC = new CtrCategory();
            var items = ctrC.GetListCategory();
            foreach (var item in items)
            {
                TreeNode node = new TreeNode();
                node.Text = item.CategoryName;
                node.Tag = item.CategoryID;
                node.ImageIndex = 0;
                treeViewCate.Nodes.Add(node);
            }
        }
        private void treeViewCate_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeViewCate.SelectedNode != null)
                treeViewCate.SelectedNode.BackColor = Color.Transparent;
        }

        private void treeViewPro_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (treeViewPro.SelectedNode != null)
                treeViewPro.SelectedNode.BackColor = Color.Transparent;
        }
        private void treeViewCate_AfterSelect(object sender, TreeViewEventArgs e)
        {
            treeViewPro.Nodes.Clear();
            var cateID = Convert.ToInt32(e.Node.Tag);
            CtrProduct ctrT = new CtrProduct();
            var items = ctrT.ProductGetByCate(cateID, -1,0);
            foreach (var item in items)
            {
                if (item.ProductID != parentProductID)
                {
                    TreeNode node = new TreeNode();
                    node.Text = item.ProductName;
                    node.Tag = item.ProductID;
                    node.ImageIndex = 0;
                    treeViewPro.Nodes.Add(node);
                }
            }
            e.Node.BackColor = Color.FromArgb(220, 220, 220);            
        }
        
        private void treeViewPro_AfterSelect(object sender, TreeViewEventArgs e)
        {
            int proID = Convert.ToInt32(e.Node.Tag);
            CtrProduct ctrp = new CtrProduct();
            productInfo = ctrp.ProductGetInfo(proID);
            lblProductName.Text = productInfo.ProductName;
            lblUnit.Text = productInfo.Unit;
            txtQuantity.Text = "10";
            e.Node.BackColor = Color.FromArgb(220, 220, 220);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (treeViewPro.SelectedNode == null)
            {
                MessageBox.Show("Bạn chưa chọn sản phẩm!", "Cảnh báo!");
                return;
            }
            CtrProduct ctrP = new CtrProduct();
            int ret=ctrP.QuantificationInsert(parentProductID, productInfo.ProductID, Convert.ToDouble(txtQuantity.Text), productInfo.Unit, txtDescription.Text);
            if (ret > 0)
            {
                CtrAdmin ctra = new CtrAdmin();
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 3, "Thêm nguyên liệu \"" + productInfo.ProductName + "\" vào định lượng mặt hàng \"" + parentName + "\"", "Định lượng");
                frmQuan.ReloadQuantification();
            }
            else
            {
                if (ret == -1)
                    MessageBox.Show("Sản phẩm đã tồn tại trong định lượng!");
                else
                    MessageBox.Show("Có lỗi xảy ra!");
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
